package com.quectel.core.module.entranceguard.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.entranceguard.dao.EntranceGuardAbilityConfigDao;
import com.quectel.core.module.entranceguard.dto.EntranceGuardAbilityConfigDto;
import com.quectel.core.module.entranceguard.entity.EntranceGuardAbilityConfigEntity;
import com.quectel.core.module.entranceguard.service.EntranceGuardAbilityConfigService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-15 14:29:44
 */
@DubboService
public class EntranceGuardAbilityConfigServiceImpl implements EntranceGuardAbilityConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EntranceGuardAbilityConfigServiceImpl.class);

    private static final String ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID:";
    private static final String ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_DEVICE_TYPE_ID = RedisCacheConstants.PROJECT_NAME + "ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_DEVICE_TYPE_ID:";

    @Autowired
    private EntranceGuardAbilityConfigDao entranceGuardAbilityConfigDao;


    @Override
    public EntranceGuardAbilityConfigDto selectCacheByDeviceTypeId(Long deviceTypeId) {
        return CacheKit.cacheToRedis(() -> {
                    EntranceGuardAbilityConfigEntity entity = entranceGuardAbilityConfigDao.selectOne(
                            new LambdaQueryWrapper<EntranceGuardAbilityConfigEntity>()
                                    .eq(EntranceGuardAbilityConfigEntity::getDeviceTypeId, deviceTypeId)
                                    .last(SqlConstants.MYSQL_LIMIT_ONE)
                    );
                    return paddingField(CopyUtils.copyObj(entity, EntranceGuardAbilityConfigDto.class));
                }, ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_DEVICE_TYPE_ID + deviceTypeId
                , SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS);
    }

    @Override
    public EntranceGuardAbilityConfigDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public EntranceGuardAbilityConfigDto selectById(Long id) {
        EntranceGuardAbilityConfigEntity entity = entranceGuardAbilityConfigDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, EntranceGuardAbilityConfigDto.class));
    }

    @Override
    public List<EntranceGuardAbilityConfigDto> queryList(Map<String, Object> params) {
        List<EntranceGuardAbilityConfigEntity> list = entranceGuardAbilityConfigDao.queryList(params);
        List<EntranceGuardAbilityConfigDto> result = CopyUtils.copyList(list, EntranceGuardAbilityConfigDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private EntranceGuardAbilityConfigDto paddingField(EntranceGuardAbilityConfigDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return entranceGuardAbilityConfigDao.queryTotal(params);
    }

    @Override
    public Long save(EntranceGuardAbilityConfigDto dto) {
        EntranceGuardAbilityConfigEntity entity = CopyUtils.copyObj(dto, EntranceGuardAbilityConfigEntity.class);
        entity.setId(Snowflake.nextId());
        entranceGuardAbilityConfigDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(EntranceGuardAbilityConfigDto dto) {
        EntranceGuardAbilityConfigEntity entity = CopyUtils.copyObj(dto, EntranceGuardAbilityConfigEntity.class);
        entranceGuardAbilityConfigDao.updateById(entity);
        CacheKit.invalidRedisCache(ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(EntranceGuardAbilityConfigDto dto) {

        EntranceGuardAbilityConfigEntity entity = CopyUtils.copyObj(dto, EntranceGuardAbilityConfigEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, entranceGuardAbilityConfigDao);

        CacheKit.invalidRedisCache(ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        entranceGuardAbilityConfigDao.deleteById(id);
        CacheKit.invalidRedisCache(ENTRANCE_GUARD_ABILITY_CONFIG_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
